package com.harry.hr.biz.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.harry.hr.biz.HumanFileBIZ;
import com.harry.hr.dao.HumanFileDAO;
import com.harry.hr.entity.HumanFile;
import com.harry.hr.entity.HumanFileDig;
import com.harry.hr.utils.FillEntityUtil;
import com.harry.hr.utils.StringUtil;
import com.sun.org.apache.bcel.internal.generic.NEW;



public class HumanFileBIZImpl implements HumanFileBIZ {

	private HumanFileDAO humanFileDAO;
	public void setHumanFileDAO(HumanFileDAO humanFileDAO) {
		this.humanFileDAO = humanFileDAO;
	}

	public boolean addHumanFile(HumanFile humanFile) {
		try {
			
			//生成人员文件号
			humanFile.setHumanId(StringUtil.createRandomPersonFileId());
			
			//条件：check以及checktime为空表示未审核
			humanFile.setCheckStatus(Short.parseShort("0"));
			humanFile.setHumanFileStatus(Byte.parseByte("1"));
			
			//注册时间为当前时间
			humanFile.setRegistTime(new Date());
			
			//执行增加人员的方法
			humanFileDAO.addHumanFile(humanFile);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean delRealHumanFile(HumanFile humanFile) {
		try {
			humanFileDAO.delHumanFile(humanFile);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public List<HumanFile> getAllHumanFile() {
		return humanFileDAO.getAllHumanFile();
	}

	public HumanFile getById(int hufId) {
		return humanFileDAO.getById(hufId);
	}

	
	
	
	
	
	public boolean doDigHumanFile(HumanFile humanFile) {
		try {			
			//审核完毕后，CheckStatus为1
			humanFile.setCheckStatus(Short.parseShort("1"));
			
			
			//审核完毕后，HumanFileStatus为1
			humanFile.setHumanFileStatus(Byte.parseByte("1"));
			
			//保存复核时间为当前时间
			humanFile.setCheckTime(new Date());
			
			//设置最后修改时间为当前时间
			humanFile.setLastlyChangeTime(new Date());
			
			//调用biz修改人员信息方法
			humanFileDAO.updateHumanFile(humanFile);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public List<HumanFile> getDigHumanFileByPage(HumanFile humanFile,
			int currentPage, int pageSize) {
		DetachedCriteria criteria=DetachedCriteria.forClass(HumanFile.class);
		
		//条件：checkStatus为0
		criteria.add(Restrictions.eq("checkStatus", Short.parseShort("0")));
		
		int firstResult=pageSize*(currentPage-1);		
		
		List<HumanFile> humanFileList=humanFileDAO.getHumanFileByPropPage(criteria, firstResult, pageSize);		
		
		return humanFileList;
	}



	public int getDigHumanFileByPageCount(HumanFile humanFile)  {
		DetachedCriteria criteria=DetachedCriteria.forClass(HumanFile.class);
		
		//条件：checkStatus为0
		criteria.add(Restrictions.eq("checkStatus", Short.parseShort("0")));
		
		return humanFileDAO.getHumanFileByPropPageCount(criteria);
	}

	public List<HumanFile> getQueryHumanFileByPage(HumanFile humanFile,
			int currentPage, int pageSize) {
		DetachedCriteria criteria=DetachedCriteria.forClass(HumanFile.class);
		
		//添加查询条件humanFileStatus为1,即人员文件状态为正常,非删除状态
		criteria.add(Restrictions.eq("humanFileStatus", Byte.parseByte("1")));
		
		//添加查询条件checkStatus为1,即符合状态为1,即
		criteria.add(Restrictions.eq("checkStatus", Short.parseShort("1")));
		
		//如果一级机构不为空或者值不等于0则添加criteria查询条件
		if(!StringUtil.validateNullorZero(humanFile.getFirstKindId())){
			criteria.add(Restrictions.eq("firstKindId", humanFile.getFirstKindId()));
		}
		
		//如果二级机构不为空或者值不等于0则添加criteria查询条件
		if(!StringUtil.validateNullorZero(humanFile.getSecondKindId())){
			criteria.add(Restrictions.eq("secondKindId", humanFile.getSecondKindId()));
		}
		
		//如果三级机构不为空或者值不等于0则添加criteria查询条件
		if(!StringUtil.validateNullorZero(humanFile.getThirdKindId())){
			criteria.add(Restrictions.eq("thirdKindId", humanFile.getThirdKindId()));
		}		
		
		//如果职位分类编号不为空或者值不等于0则添加criteria查询条件
		if(!StringUtil.validateNullorZero(humanFile.getHumanMajorKindId())){
			criteria.add(Restrictions.eq("humanMajorKindId", humanFile.getHumanMajorKindId()));
		}
		
		//如果职位编号不为空或者值不等于0则添加criteria查询条件
		if(!StringUtil.validateNullorZero(humanFile.getHumanMajorId())){
			criteria.add(Restrictions.eq("humanMajorId", humanFile.getHumanMajorId()));
		}		
		
		//如果注册日期范围查询不为空则添加criteria查询条件
		if(null!=humanFile.getQueryBeginDate()&&null!=humanFile.getQueryEndDate()){
			criteria.add(Restrictions.between("registTime", humanFile.getQueryBeginDate(), humanFile.getQueryEndDate()));
		}
		
		//设置当前页面起始记录
		int firstResult=pageSize*(currentPage-1);		
		
		//调用dao查询页面的方法
		List<HumanFile> humanFileList=humanFileDAO.getHumanFileByPropPage(criteria, firstResult, pageSize);		
		return humanFileList;
	}

	
	public int getQueryHumanFileByPageCount(HumanFile humanFile) {		
		DetachedCriteria criteria=DetachedCriteria.forClass(HumanFile.class);
		
		criteria.add(Restrictions.eq("humanFileStatus", Byte.parseByte("1")));
		criteria.add(Restrictions.eq("checkStatus", Short.parseShort("1")));
		
		//添加查询条件humanFileStatus为1,即人员文件状态为正常,非删除状态
		criteria.add(Restrictions.eq("humanFileStatus", Byte.parseByte("1")));
		
		//添加查询条件checkStatus为1,即符合状态为1,即
		criteria.add(Restrictions.eq("checkStatus", Short.parseShort("1")));
		
		//如果一级机构不为空或者值不等于0则添加criteria查询条件
		if(!StringUtil.validateNullorZero(humanFile.getFirstKindId())){
			criteria.add(Restrictions.eq("firstKindId", humanFile.getFirstKindId()));
		}
		
		//如果二级机构不为空或者值不等于0则添加criteria查询条件
		if(!StringUtil.validateNullorZero(humanFile.getSecondKindId())){
			criteria.add(Restrictions.eq("secondKindId", humanFile.getSecondKindId()));
		}
		
		//如果三级机构不为空或者值不等于0则添加criteria查询条件
		if(!StringUtil.validateNullorZero(humanFile.getThirdKindId())){
			criteria.add(Restrictions.eq("thirdKindId", humanFile.getThirdKindId()));
		}		
		
		//如果职位分类编号不为空或者值不等于0则添加criteria查询条件
		if(!StringUtil.validateNullorZero(humanFile.getHumanMajorKindId())){
			criteria.add(Restrictions.eq("humanMajorKindId", humanFile.getHumanMajorKindId()));
		}
		
		//如果职位编号不为空或者值不等于0则添加criteria查询条件
		if(!StringUtil.validateNullorZero(humanFile.getHumanMajorId())){
			criteria.add(Restrictions.eq("humanMajorId", humanFile.getHumanMajorId()));
		}		
		
		//如果注册日期范围查询不为空则添加criteria查询条件
		if(null!=humanFile.getQueryBeginDate()&&null!=humanFile.getQueryEndDate()){
			criteria.add(Restrictions.between("registTime", humanFile.getQueryBeginDate(), humanFile.getQueryEndDate()));
		}
			
	
		
		return humanFileDAO.getHumanFileByPropPageCount(criteria);
	
	}
	
	
	
	
	
	
	
	
	
	
	
	
	public boolean doEditHumanFile(HumanFile humanFile) {
		HumanFile tempHumanFile=humanFileDAO.getById(humanFile.getHufId());
		HumanFileDig humanFileDig=new HumanFileDig();
		
		//修改后重新文件审核状态改为未审核状态
		humanFile.setCheckStatus(Short.parseShort("0"));
		
		convert(tempHumanFile, humanFileDig);
		
		//设置修改时间默认为当前时间
		humanFile.setChangeTime(new Date());
		
		//最后修改时间设置为当前时间
		humanFile.setLastlyChangeTime(new Date());
		
		try {
			humanFileDAO.addDigHumanFile(humanFileDig);
			humanFileDAO.updateHumanFile(humanFile);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}	
		
		return false;
	}


	public HumanFileDig convert(HumanFile source,HumanFileDig target){
		target.setHumanId(source.getHumanId());
		target.setFirstKindId(source.getFirstKindId());
		target.setFirstKindName(source.getFirstKindName());
		target.setSecondKindId(source.getSecondKindId());
		target.setSecondKindName(source.getSecondKindName());
		target.setThirdKindId(source.getThirdKindId());
		target.setThirdKindName(source.getThirdKindName());
		target.setHumanName(source.getHumanName());
		target.setHumanAddress(source.getHumanAddress());
		target.setHumanPostcode(source.getHumanPostcode());
		target.setHumanProDesignation(source.getHumanProDesignation());
		target.setHumanMajorId(source.getHumanMajorKindId());
		target.setHumanMajorKindName(source.getHumanMajorKindName());
		target.setHumanMajorId(source.getHumanMajorId());
		target.setHumanMajorName(source.getHumanMajorName());
		target.setHumanTelephone(source.getHumanTelephone());
		target.setHumanMobilephone(source.getHumanMobilephone());
		target.setHumanBank(source.getHumanBank());
		target.setHumanAccount(source.getHumanAccount());
		target.setHumanQq(source.getHumanQq());
		target.setHumanEmail(source.getHumanEmail());
		target.setHumanHobby(source.getHumanHobby());
		target.setHumanSpeciality(source.getHumanSpeciality());
		target.setHumanSex(source.getHumanSex());
		target.setHumanReligion(source.getHumanReligion());
		target.setHumanParty(source.getHumanParty());
		target.setHumanNationality(source.getHumanNationality());
		target.setHumanRace(source.getHumanRace());
		target.setHumanBirthday(source.getHumanBirthday());
		target.setHumanBirthplace(source.getHumanBirthplace());
		target.setHumanAge(source.getHumanAge());
		target.setHumanEducatedDegree(source.getHumanEducatedDegree());
		target.setHumanEducatedYears(source.getHumanEducatedYears());
		target.setHumanEducatedMajor(source.getHumanEducatedMajor());
		target.setHumanSocietySecurityId(source.getHumanSocietySecurityId());
		target.setHumanIdCard(source.getHumanIdCard());
		target.setRemark(source.getRemark());
		target.setSalaryStandardId(source.getSalaryStandardId());
		target.setSalaryStandardName(source.getSalaryStandardName());
		target.setSalarySum(source.getSalarySum());
		target.setDemandSalaraySum(source.getDemandSalaraySum());
		target.setDemandSalaraySum(source.getPaidSalarySum());
		target.setMajorChangeAmount(source.getMajorChangeAmount());
		target.setBonusAmount(source.getBonusAmount());
		target.setTrainingAmount(source.getTrainingAmount());
		target.setFileChangAmount(source.getFileChangAmount());
		target.setHumanHistroyRecords(source.getHumanHistroyRecords());
		target.setHumanFamilyMembership(source.getHumanFamilyMembership());
		target.setHumanPicture(source.getHumanPicture());
		target.setAttachmentName(source.getAttachmentName());
		target.setCheckStatus(source.getCheckStatus());
		target.setRegister(source.getRegister());
		target.setChecker(source.getChecker());
		target.setChanger(source.getChanger());
		target.setRegistTime(source.getRegistTime());
		target.setCheckTime(source.getCheckTime());
		target.setChangeTime(source.getChangeTime());
		target.setChangeTime(source.getLastlyChangeTime());
		target.setDeleteTime(source.getDeleteTime());
		target.setRecoveryTime(source.getRecoveryTime());
		target.setHumanFileStatus(source.getHumanFileStatus());	
		
		return target;
	}

	
	/**
	 * 执行人员删除的方法（假删除） 
	 */	
	public boolean doDelTempHumanFile(String hufId) {
		//创建人员修改备份对象
		HumanFileDig humanFileDig=new HumanFileDig();		
		
		//根据hufId找到humanFile对象
		HumanFile humanFile=this.humanFileDAO.getById(Integer.parseInt(hufId));
				
		//把删除信息先存到到tempHumanFile对象
		HumanFile tempHumanFile=humanFileDAO.getById(humanFile.getHufId());		
		convert(tempHumanFile, humanFileDig);
		
		//设置删除时间为当前时间
		humanFile.setDeleteTime(new Date());
		
		//设置恢复时间为null
		humanFile.setRecoveryTime(null);
			
		//设置人员文件对象状态为0,即删除状态
		humanFile.setHumanFileStatus(Byte.parseByte("0"));
		
		//设置最后修改时间为当前时间
		humanFile.setLastlyChangeTime(new Date());
		
		try {
			//调用业务层执行备份删除信息的方法
			this.humanFileDAO.addDigHumanFile(humanFileDig);
			
			//调用业务层执行假删除的方法
			this.humanFileDAO.updateHumanFile(humanFile);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return false;
	}
	
	public boolean doRecoverTempHumanFile(String hufId) {
		//创建人员修改备份对象
		HumanFileDig humanFileDig=new HumanFileDig();		
		
		//根据hufId找到humanFile对象
		HumanFile humanFile=this.humanFileDAO.getById(Integer.parseInt(hufId));
				
		//把删除信息先存到到tempHumanFile对象
		HumanFile tempHumanFile=humanFileDAO.getById(humanFile.getHufId());		
		convert(tempHumanFile, humanFileDig);
		
		//设置删除时间为当前时间
		humanFile.setDeleteTime(null);
		
		//设置恢复时间为当前时间
		humanFile.setRecoveryTime(new Date());
		
		//设置最后修改时间为当前时间
		humanFile.setLastlyChangeTime(new Date());
			
		//设置人员文件对象状态为1,即恢复删除状态为正常状态
		humanFile.setHumanFileStatus(Byte.parseByte("1"));
		
		try {
			//调用业务层执行备份删除信息的方法
			this.humanFileDAO.addDigHumanFile(humanFileDig);
			
			//调用业务层执行假删除的方法
			this.humanFileDAO.updateHumanFile(humanFile);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return false;
	}
	
	
	
	

	public List<HumanFile> getDelQueryHumanFileByPage(HumanFile humanFile,
			int currentPage, int pageSize) {
		DetachedCriteria criteria=DetachedCriteria.forClass(HumanFile.class);
		
		//状态为“待复核”的员工档案不能删除
		criteria.add(Restrictions.ne("checkStatus", Short.parseShort("0")));
		
		//角色为“系统管理员”的档案不能删除
		
		
		if(!StringUtil.validateNullorZero(humanFile.getFirstKindId())){
			criteria.add(Restrictions.eq("firstKindId", humanFile.getFirstKindId()));
		}
		
		if(!StringUtil.validateNullorZero(humanFile.getSecondKindId())){
			criteria.add(Restrictions.eq("secondKindId", humanFile.getSecondKindId()));
		}
		
		if(!StringUtil.validateNullorZero(humanFile.getThirdKindId())){
			criteria.add(Restrictions.eq("thirdKindId", humanFile.getThirdKindId()));
		}		
		
		if(!StringUtil.validateNullorZero(humanFile.getHumanMajorKindId())){
			criteria.add(Restrictions.eq("humanMajorKindId", humanFile.getHumanMajorKindId()));
		}
		
		if(!StringUtil.validateNullorZero(humanFile.getHumanMajorId())){
			criteria.add(Restrictions.eq("humanMajorId", humanFile.getHumanMajorId()));
		}
				
		if(null!=humanFile.getQueryBeginDate()&&null!=humanFile.getQueryEndDate()){
			criteria.add(Restrictions.between("registTime", humanFile.getQueryBeginDate(), humanFile.getQueryEndDate()));
		}
		
		int firstResult=pageSize*(currentPage-1);	
		
		List<HumanFile> humanFileList=humanFileDAO.getHumanFileByPropPage(criteria, firstResult, pageSize);	
		
		return humanFileList;
	}

	public int getDelQueryHumanFileByPageCount(HumanFile humanFile) {
DetachedCriteria criteria=DetachedCriteria.forClass(HumanFile.class);
		
		//状态为“待复核”的员工档案不能删除
		criteria.add(Restrictions.ne("checkStatus", Short.parseShort("0")));
		
		//角色为“系统管理员”的档案不能删除
		
		
		if(!StringUtil.validateNullorZero(humanFile.getFirstKindId())){
			criteria.add(Restrictions.eq("firstKindId", humanFile.getFirstKindId()));
		}
		
		if(!StringUtil.validateNullorZero(humanFile.getSecondKindId())){
			criteria.add(Restrictions.eq("secondKindId", humanFile.getSecondKindId()));
		}
		
		if(!StringUtil.validateNullorZero(humanFile.getThirdKindId())){
			criteria.add(Restrictions.eq("thirdKindId", humanFile.getThirdKindId()));
		}		
		
		if(!StringUtil.validateNullorZero(humanFile.getHumanMajorKindId())){
			criteria.add(Restrictions.eq("humanMajorKindId", humanFile.getHumanMajorKindId()));
		}
		
		if(!StringUtil.validateNullorZero(humanFile.getHumanMajorId())){
			criteria.add(Restrictions.eq("humanMajorId", humanFile.getHumanMajorId()));
		}
				
		if(null!=humanFile.getQueryBeginDate()&&null!=humanFile.getQueryEndDate()){
			criteria.add(Restrictions.between("registTime", humanFile.getQueryBeginDate(), humanFile.getQueryEndDate()));
		}		
		return humanFileDAO.getHumanFileByPropPageCount(criteria);
	}
	
	
	








	
	
	


}
